% Figure 2. US inflation 1950M1-2018M12 and model prediction
% October 2022
% Makoto Nirei

% Need source files: US data file CPIupto2018.mat
% and simulated data generated by simulation.m 

load('CPIupto2018.mat')
p=CPIupto2018(173,:);
p2=reshape(p,12,69);
p3=diff(log(p2'))';
dp=p3(:);
dp2=reshape(dp, 24, 34);
mPUS=mean(dp2); % generates 31 points plus 3 NaN 
sPUS=std(dp2);

set(gca,'FontSize',16);
load simu.mat;
%post_simulation;

figure(4),
plot(piv,stdP,'b-d','LineWidth',2);
hold on
plot(mPUS,sPUS,'o');
year ={' 51/52',' 53/54',' 55/56',' 57/58',' 59/60',' 61/62',' 63/64',' 65/66',' 67/68',' 69/70',' 71/72',' 73/74',' 75/76',' 77/78',' 79/80',' 81/82',' 83/84',' 85/86',' 87/88',' 89/90',' 91/92',' 93/94',' 95/96',' 97/98',' 99/00',' 01/02',' 03/04',' 05/06',' 07/08',' 09/10',' 11/12',' 13/14',' 15/16',' 17/18'};
text(mPUS,sPUS,num2cell(year),'FontSize',16);
xlabel('Average inflation rate','FontSize',20);
ylabel('Standard deviation','FontSize',20);
xlim([0 0.12]);
grid;
ax=gca;
ax.FontSize=20;
legend('Model prediction','US data','Location','Northwest');
%f=gcf;
%exportgraphics(f,'fig_US_stdP.png','Resolution',300);

%% R-squared

xpiv=[0 piv]; xstdP=[0 stdP];
yUS=sPUS(4:34); xUS=mPUS(4:34);
yhat=interp1(xpiv,xstdP,xUS);
numR=sum((yUS-yhat).^2);
denR=sum((yUS-mean(yUS)).^2);
R2=1-numR/denR;